﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using LyData.Data;

public partial class Admin_CodeEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.CssClass = "errorMsg";
        this.Msg.Visible = false;
        if (!base.IsPostBack)
        {
            this.Bind();
        }
        else
        {
            this.Msg.CssClass = "msg";
        }
    }

    private void Bind()
    {
        string strwhere = "1=0";
        if(Request.Params["cid"]!=null)
        {
            if(WebPublic.WebPublic.IsNumber(Request.Params["cid"].ToString()))
            {
                strwhere = string.Format("cid={0}", Request.Params["cid"].ToString());
            }
        }
        IDb db = new DbFactory().DriverDb();
        db.Open();
        DataRow row = db.TopRow("*", "CodeList", strwhere, "codeClassid Asc,Cid Desc");
        db.Close();

        if (row != null)
        {
            txtTitle.Text = row["Title"].ToString();
            txturl.Text = row["Curl"].ToString();
            this.txtorder.Text = row["Weight"].ToString();
            this.PicSmallHyperLink.NavigateUrl = row["imgpath"].ToString();
            this.AddTime.Text = row["AddTime"].ToString();
            this.ArticleClassIdList.SelectedValue = row["codeClassid"].ToString();
            this.txtwidth.Text = row["cwidth"].ToString();
            this.txtheight.Text = row["cheight"].ToString();
        }
        else
        {
            WebPublic.WebPublic.ShowAndRedirect(this,"暂无记录","CodeList.aspx");
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            Convert.ToDateTime(this.AddTime.Text);
            args.IsValid = true;
        }
        catch
        {
            args.IsValid = false;
        }
    }

    protected void PicSmallCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (this.PicSmall.HasFile)
        {
            string extension = FSO.GetExtension(this.PicSmall.FileName);
            string extensionList = ".jpg|.gif|.png|.bmp";
            args.IsValid = FSO.ExtensionValid(extension, extensionList);
            this.PicSmallCustomValidator.ErrorMessage = "小图格式错误，合法格式：" + extensionList;
        }
        else
        {
            this.PicSmallCustomValidator.ErrorMessage = "小图路径选择错误！";
            args.IsValid = false;
        }
    }



    protected void Edit_Click(object sender, EventArgs e)
    {
        if (this.PicSmall.HasFile)
        {
            string path = string.Format("{0}{1}/{2}", FSO.GetUploadPath(), "Ad", FSO.RandomFileName(this.PicSmall.FileName));
            string filePath = base.Server.MapPath(path);
            FSO.PathExists(filePath);
            this.PicSmall.SaveAs(filePath);
            this.PicSmallHyperLink.NavigateUrl = path;
        }

        string[,] updatedeColumns = new string[,] {
                        { "imgpath", PicSmallHyperLink.NavigateUrl, "NText", "0" }, 
                        { "codeClassid", this.ArticleClassIdList.SelectedValue.ToString(), "int", "0" }, 
                        { "Title", this.txtTitle.Text, "NVarChar", "0" },                         
                        { "AddTime", this.AddTime.Text, "DateTime", "0" },                         
                        { "Curl",txturl.Text,"NText","0" },
                        { "Weight", this.txtorder.Text,"int","32" },
                        { "cwidth", this.txtwidth.Text,"int","32" },
                        { "cheight", this.txtheight.Text,"int","32" }
                    };

        IDb db = new DbFactory().DriverDb();
        db.Open();
        db.Update("Codelist", updatedeColumns, string.Format("cid={0}", Request.Params["cid"].ToString()));
        db.Close();
        WebPublic.WebPublic.ShowAndRedirect(this, "更新成功", "CodeList.aspx");
    }
}
